package com.example.finalwork;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.finalwork.DAO.UserDAO;
import com.example.finalwork.lmpl.UserDAOlmpl;
import com.example.finalwork.DB.DataBaseHelper;
import com.example.finalwork.Entity.User;

import java.util.List;


public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
    private EditText UserNameText,PasswordText;
    public static String data;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        findViewById(R.id.Login_button).setOnClickListener(this);
        findViewById(R.id.register_button).setOnClickListener(this);
        findViewById(R.id.change_button).setOnClickListener(this);
        UserNameText = findViewById(R.id.UserNameText);
        PasswordText = findViewById(R.id.PasswordText);
        setTitle("登录界面");

        //创建数据库
        DataBaseHelper userhelper = new DataBaseHelper(this);
        userhelper.getWritableDatabase();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.Login_button:
                if(UserNameText.getText().toString().equals("") || PasswordText.getText().toString().equals(""))
                {
                    Toast.makeText(this,"用户名或密码不能为空",Toast.LENGTH_SHORT).show();
                    break;
                }
                UserDAO userDAO = new UserDAOlmpl(this);
                List<User> users = userDAO.GetAllUser();
                for(int i = 0 ; i < users.size() ; i++)
                {
                    User olduser = users.get(i);
                    if(UserNameText.getText().toString().equals(olduser.getUserName()) && PasswordText.getText().toString().equals(olduser.getPassWord()))
                    {
                        Toast.makeText(this,"登录成功",Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(this,MainActivity.class);
                        String UserName = UserNameText.getText().toString();
                        data = UserName;
                        startActivity(intent);
                        return;
                    }
                }
                Toast.makeText(this,"用户名或密码不正确",Toast.LENGTH_SHORT).show();
                break;
            case R.id.register_button:
                Toast.makeText(this,"进入新用户注册界面",Toast.LENGTH_SHORT).show();
                startActivity(new Intent(this,RegisterActivity.class));
                break;
            case R.id.change_button:
                Toast.makeText(this,"进入修改密码界面",Toast.LENGTH_SHORT).show();
                startActivity(new Intent(this, ChangePdActivity.class));
                break;
        }
    }

}